import { createRouter, createWebHistory } from 'vue-router'
import {ElMessage} from "element-plus";
const homeview = () => import("../views/HomeView.vue");
const login = ()=> import("../views/Login/Index.vue")
const regist =()=> import("../views/Regist/Index.vue")
const index = ()=>import('../views/HomeIndex/Index.vue')
const indexPage = () =>import('../views/IndexPage/Index.vue')
const introPage = () => import('../views/IntroducePage/Index.vue')
const teamPage =() => import('../views/TeamPage/Index.vue')
const doctorInfo =()=>import('../views/DoctorInfo/Index.vue')
const privateInfo = ()=>import('../views/PrivateInfo/Index.vue')
const billPage = () => import('../views/BillPage/Index.vue')
const appointPage =() => import('../views/AppointPage/Index.vue')
const orderHistoryPage =()=>import('../views/OrderHistoryPage/Index.vue')
const emergencyPage =()=>import('../views/EmergencyPage/Index.vue')
const feedbackPage = ()=>import('../views/FeedBack/Index.vue')
const guidePage =()=> import('../views/GuidePage/Index.vue')
const newsPage =()=>import('../views/News/Index.vue')
const progressPage =()=>import('../views/ProgressPage/Index.vue')
const orderHistoryDetailedPage = ()=>import('../views/OrderHisoryDetailedPage/Index.vue')
const infoCenterPage= ()=> import('../views/InfoCenter/Index.vue')
const unreadPage = ()=> import('../views/unreadInfo/Index.vue')
const readPage =()=> import('../views/readInfo/Index.vue')
const binPage =()=>import('../views/BinPage/Index.vue')
const payBillPage = ()=>import('../views/Reception/paybill.vue')

const routes = [
    { path: "/back", redirect: "/homeview" },

    {
        path: '/doctor',
        name: 'doctor',
        component: doctorInfo
    },
    {
        path: '/user/private',
        name: 'private',
        meta: {
            auth: true
        },
        component: privateInfo,
    },

    {
        path: '/user/infoCenter',
        redirect: '/user/infoCenter/unread',
        name: 'info_center',
        meta: {
            auth: true
        },
        component: infoCenterPage,
        children: [{
            path: 'unread',
            name: 'unreadInfo',
            component: unreadPage,
            meta: {
                auth: true
            }
        },
            {
                path: 'read',
                name: 'readInfo',
                component: readPage,
                meta: {
                    auth: true
                }
            },
            {
                path: 'bin',
                name: 'bin',
                component: binPage,
                meta: {
                    auth: true
                }
            }
        ]
    },

    {
        path: '/user/passChange',
        name: 'passChange',
        redirect: '/user/private',
        meta: {
            auth: true
        },
        component: privateInfo
    },
    {
        path: '/user/orderDetails',
        name: 'order_history_detailed',
        component: orderHistoryDetailedPage,
        meta: {
            auth: true
        },
    },
    { path: '/', redirect: '/index/home' },
    {
        path: '/index',
        name: 'index',
        component: index,
        children: [

            {
                path: '/index/introduce',
                name: 'introduce',
                component: introPage
            },
            {
                path: '/index/home',
                name: 'home',
                component: indexPage
            },
            {
                path: '/index/team',
                name: 'team',
                component: teamPage,
            },
            {
                path: '/index/bill',
                name: 'bill',
                component: billPage,
                meta: {
                    auth: true
                },
            },
            {
                path: '/index/appoint',
                name: 'appoint-page',
                component: appointPage,
                meta: {
                    auth: true
                },
            },
            {
                path: '/index/orderHistory',
                name: 'order-history',
                component: orderHistoryPage,
                meta: {
                    auth: true
                },
            },
            {
                path: '/index/emergency',
                name: 'emergency',
                component: emergencyPage
            },
            {
                path: '/index/feedback',
                name: 'feedback',
                component: feedbackPage
            },
            {
                path: '/index/guide',
                name: 'guide',
                component: guidePage
            },
            {
                path: '/index/news',
                name: 'news',
                component: newsPage
            },
            {
                path: '/index/progress',
                name: 'progress',
                component: progressPage,
                meta: {
                    auth: true
                },
            }
        ]
    },


    {
        path: "/homeview",
        name: "homeview",
        component: homeview,
        children: [
            {
                path: "board1",
                name: "board1",
                component: () =>
                    import ("../views/board1.vue")
            },

            {
                path: "takeMedicine",
                name: "takeMedicine",
                component: () =>
                    import ("../views/Pharmacist/takeMedicine.vue")
            },

            {
                path: "medicineList",
                name: "medicineList",
                component: () =>
                    import ("../views/Pharmacist/medicineList.vue")
            },

            {
                path: "staffList",
                name: "staffList",
                component: () =>
                    import ("../views/Admin/staffList.vue")
            },

            {
                path: "subject",
                name: "subject",
                component: () =>
                    import ("../views/Admin/subject.vue")
            },

            {
                path: "newsList",
                name: "newsList",
                component: () =>
                    import ("../views/Admin/newsList.vue")
            },

            {
                path: "finance",
                name: "finance",
                component: () =>
                    import ("../views/Admin/finance.vue")
            },

            {
                path: "roleList",
                name: "roleList",
                component: () =>
                    import ("../views/Admin/roleList.vue")
            },

            {
                path: "permission",
                name: "permission",
                component: () =>
                    import ("../views/Admin/permission.vue")
            },

            {
                path: "rolePermission",
                name: "rolePermission",
                component: () =>
                    import ("../views/Admin/rolePermission.vue")
            },

            {
                path: "patientList",
                name: "patientList",
                component: () =>
                    import ("../views/Doctor/patientList.vue")
            },

            {
                path: "medicineRecord",
                name: "medicineRecord",
                component: () =>
                    import ("../views/Pharmacist/medicineRecord.vue")
            },
            {
                path: "staffInfo",
                name: "staffInfo",
                component: () =>
                    import ("../views/StaffInformation/staffInfo.vue")
            },
            {
                path: "patientInfo",
                name: "patientInfo",
                component: () =>
                    import ("../views/Doctor/patientInfo.vue")
            },
            {
                path: "drugList",
                name: "drugList",
                component: () =>
                    import ("../views/Doctor/drugList.vue")
            },

            {
                path: "appointment",
                name: "appointment",
                component: () =>
                    import ("../views/Reception/appointment.vue"),
            },

            {
                path: "payBill",
                name: 'paybill',
                component: payBillPage
            }
        ]
    },


    { path: "/login", name: 'login', component: login },

    { path: "/regist", name: 'regist', component: regist },

]

export const router = createRouter({
    history: createWebHistory(),
    routes: routes,
})

router.beforeEach((to, from, next) => {
    let token = localStorage.getItem("satoken")
    document.querySelector('body').setAttribute('style', 'margin:0;padding:0;')
    if (to.meta.auth) {
        if (token) {
            next()
        } else {
            ElMessage.warning({
                type: 'warning',
                message: '请先登录！'
            })
            next({
                name: 'home'
            })
            if (to.name === 'private' || to.name === 'passChange') {
                next({
                    name: 'home'
                })
            }
        }
    } else {
        next()
    }
})